/**
 * @param {number[]} nums
 * @return {number}
 */
var pivotIndex = function(nums) {
  let sum = nums.reduce((preVal, curVal) => {
    return preVal + curVal;
  }), compute = 0;
  for(let i = 0; i < nums.length; i++) {
    if(compute * 2 + nums[i] == sum) return i;
    compute += nums[i];
  }
  return -1;
};
let nums = [1, 7, 3, 6, 5, 6];
console.log(pivotIndex(nums));
nums = [1, 2, 3];
console.log(pivotIndex(nums));
nums = [2, 1, -1];
console.log(pivotIndex(nums));
nums = [-1,-1,-1,-1,-1,0];
console.log(pivotIndex(nums));
